#include <QtGui>

#include "mapItem.h"

MapItem::MapItem(DiagramType diagramType, QMenu *contextMenu,
             QGraphicsItem *parent, QGraphicsScene *scene)
    : QGraphicsPixmapItem(parent, scene)
{
    myDiagramType = diagramType;
    myContextMenu = contextMenu;
	entryId = -1;
    QPainterPath path;
    switch (myDiagramType) {
        case StartEnd:
            path.moveTo(200, 50);
            path.arcTo(150, 0, 50, 50, 0, 90);
            path.arcTo(50, 0, 50, 50, 90, 90);
            path.arcTo(50, 50, 50, 50, 180, 90);
            path.arcTo(150, 50, 50, 50, 270, 90);
            path.lineTo(200, 25);
            myPolygon = path.toFillPolygon();
            break;
		case RoadSolidVertical:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/vert.png"));
            break;
		case RoadSolidHorizontal:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/horiz.png"));
            break;
		case RoadEmpty:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/simple_road.png"));
            break;
		case CornerDL:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/down_left.png"));
			break;
		case CornerDR:
			myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/down_right.png"));
			break;
		case CornerUL:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/up_left.png"));
			break;
		case CornerUR:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/up_right.png"));
			break;
		case DashU:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/dashes_up.png"));
			break;
		case DashD:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/dashes_down.png"));
			break;
		case DashR:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/dashes_right.png"));
			break;
		case DashL:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/dashes_left.png"));
			break;
		case SolidU:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/up.png"));
			break;
		case SolidD:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/down.png"));
			break;
		case SolidR:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/right.png"));
			break;
		case SolidL:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/left.png"));
			break;
		case IntersectionR:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/horiz_give_way_right.png"));
			break;
		case IntersectionL:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/horiz_give_way_left.png"));
			break;
		case IntersectionU:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/vert_give_way_up.png"));
			break;
		case IntersectionD:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/vert_give_way_down.png"));
			break;
		case Start:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/entry.png"));
			break;
		case End:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/exit.png"));
			break;
        case Step:
            myPolygon << QPointF(0, 0) << QPointF(160, 0)
                      << QPointF(160, 160) << QPointF(0, 160)
                      << QPointF(0, 0);
			setPixmap(QPixmap(":/Resources/road_v.png"));
            break;
		case Path:
            myPolygon << QPointF(-100, 0) << QPointF(0, 100)
                      << QPointF(100, 0) << QPointF(0, -100)
                      << QPointF(-100, 0);
			setPixmap(QPixmap(":/images/path.png"));
			
			break;
        default:
            myPolygon << QPointF(-120, -80) << QPointF(-70, 80)
                      << QPointF(120, 80) << QPointF(70, -80)
                      << QPointF(-120, -80);
            break;
    }
    //setPolygon(myPolygon);
	//setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
}


QPixmap MapItem::image() const
{
    QPixmap pixmap(250, 250);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setPen(QPen(Qt::black, 8));
    painter.translate(125, 125);
    painter.drawPolyline(myPolygon);
	if(myDiagramType == RoadEmpty)
	{
		return QPixmap(":/images/simple_road.png");
	}
	else if(myDiagramType == RoadSolidVertical)
	{
		return QPixmap(":/images/vert.png");
	}
	else if(myDiagramType == RoadSolidHorizontal)
	{
		return QPixmap(":/images/horiz.png");
	}
	else if(myDiagramType == CornerUL)
	{
		return QPixmap(":/images/up_left.png");
	}
	else if(myDiagramType == CornerUR)
	{
		return QPixmap(":/images/up_right.png");
	}
	else if(myDiagramType == CornerDL)
	{
		return QPixmap(":/images/down_left.png");
	}
	else if(myDiagramType == CornerDR)
	{
		return QPixmap(":/images/down_right.png");
	}
	else if(myDiagramType == DashD)
	{
		return QPixmap(":/images/dashes_down.png");
	}
	else if(myDiagramType == DashL)
	{
		return QPixmap(":/images/dashes_left.png");
	}
	else if(myDiagramType == DashU)
	{
		return QPixmap(":/images/dashes_up.png");
	}
	else if(myDiagramType == DashR)
	{
		return QPixmap(":/images/dashes_right.png");
	}
	else if(myDiagramType == SolidR)
	{
		return QPixmap(":/images/right.png");
	}
	else if(myDiagramType == SolidL)
	{
		return QPixmap(":/images/left.png");
	}
	else if(myDiagramType == SolidU)
	{
		return QPixmap(":/images/up.png");
	}
	else if(myDiagramType == SolidD)
	{
		return QPixmap(":/images/down.png");
	}
	else if(myDiagramType == IntersectionR)
	{
		return QPixmap(":/images/horiz_give_way_right.png");
	}
	else if(myDiagramType == IntersectionL)
	{
		return QPixmap(":/images/horiz_give_way_left.png");
	}
	else if(myDiagramType == IntersectionU)
	{
		return QPixmap(":/images/vert_give_way_up.png");
	}
	else if(myDiagramType == IntersectionD)
	{
		return QPixmap(":/images/vert_give_way_down.png");
	}
	else if(myDiagramType == Start)
	{
		return QPixmap(":/images/entry.png");
	}
	else if(myDiagramType == End)
	{
		return QPixmap(":/images/exit.png");
	}
	else
	{
		return QPixmap(":/Resources/road_v.png");
	}
    //return pixmap;
}

void MapItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
    scene()->clearSelection();
    setSelected(true);
   // myContextMenu->exec(event->screenPos());
}

QVariant MapItem::itemChange(GraphicsItemChange change,
                     const QVariant &value)
{
    if (change == QGraphicsItem::ItemPositionChange) {

    }

    return value;
}
